<?php
/**
 * 展馆
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/28
 * Time: 22:02
 */
namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use Think\Db;

class Pavilion extends ApiBase
{
    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        $this->model = model('Pavilion');
    }

    /**
     * 展馆列表
     */
    public function getList(){
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');

        $page = max(1,intval($page));
        $pagesize = 10;

        if(!empty(input('exhibition_id'))){
            $where['exhibition_id'] = input('exhibition_id');
        }

        $where['a.is_delete'] = 0;
        $wheres['is_delete'] = 0;
        if(!empty($keyword)){
            $wheres['title'] = $where['a.title'] = array('like','%'.$keyword.'%');
        }

        //$list = $this->model->listWherePage($where,'id desc',($page-1)*$pagesize,$pagesize);
        $list = db('Pavilion')->alias('a')->field('a.*,b.title as exh_title')->join('exhibition b','a.exhibition_id=b.id','left')->where($where)->order('id desc')->limit(($page-1)*$pagesize.','.$pagesize)->select();
        $total = db('Pavilion')->alias('a')->field('a.*,b.title as exh_title')->join('exhibition b','a.exhibition_id=b.id','left')->where($where)->count();

        if(!empty($list)){
            foreach ($list as &$v){
                $v['exhibition_info'] = $this->model->findExhibition($v['id']);
            }
            unset($v);
        }

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }

    /**
     * 展馆详情
     * @return \think\response\Json|void
     */
    public function detail(){
        $id = $this->request->param('id');

        $where['id'] = $id;
        $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     *
     * @return mixed
     */
    public function _before_add(){
        $data = [];
        if(!empty(input('title'))){
            $data['title'] = input('title');
        }

        if(!empty(input('address'))) {
            $data['address'] = input('address');
        }

        if(!empty(input('area'))) {
            $data['area'] = input('area');
        }
        if(!empty(input('link'))) {
            $data['link'] = input('link');
        }
        if(!empty(input('contact'))) {
            $data['contact'] = input('contact');
        }
        if(!empty(input('mobile'))) {
            $data['mobile'] = input('mobile');
        }

        if(!empty(input('exhibition_id'))) {
            $data['exhibition_id'] = input('exhibition_id');
        }

        return $data;
    }

    /**
     * 新增展馆
     * @return \think\response\Json|void
     */
    public function add(){
        $data = $this->_before_add();
        $data['create_time']  = time();
        $data['is_delete']  = 0;

        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功','data'=>$r]);
    }

    /**
     * 编辑展馆
     * @return \think\response\Json|void
     */
    public function edit(){
        $id = $this->request->param('id');
        $data = $this->_before_add();

        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除展馆
     * @return \think\response\Json|void
     */
    public function del(){
        $ids = $this->request->param('id');
        $id = explode(',',$ids);
        $is=true;
        //首先进行数据验证
        foreach ($id as $val){
            $where = ['id'=>['in',$val],'is_delete'=>0];
            $r = $this->model->countWhere($where);
            if(empty($r)){
                $is=false;
                break;
            }
        }
        if(!$is){
            return json(['code'=>0,'msg'=>'记录不存在，请核实']);
        }
        $msg='';
        foreach ($id as $v){
            $r = $this->model->deleteWhere(['id'=>$v]);
            if(!$r){
                $is=false;
                $msg='id:'.$v.'操作失败';
            }
        }

        if(!$is){
            Db::rollback();
            return json(['code'=>0,'msg'=>$msg]);
        }
        Db::commit();
        return json(['code'=>1,'msg'=>'操作成功']);
    }




}